package com.lshtv.dao;

// default package

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lshtv.base.PageList;
import com.lshtv.model.Work;

/**
 	* A data access object (DAO) providing persistence and search support for Work entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Work
  * @author MyEclipse Persistence Tools 
 */

public class WorkDAO extends HibernateDaoSupport {
    private static final Logger log         = LoggerFactory.getLogger(WorkDAO.class);
    //property constants
    public static final String  TITLE       = "title";
    public static final String  DESCRIPTION = "description";
    public static final String  LINK        = "link";
    public static final String  TYPE        = "type";
    public static final String  FILES       = "files";
    public static final String  ITEM        = "item";
    public static final String  SEQUENCE    = "sequence";

    public void save(Work transientInstance) {
        log.debug("saving Work instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(Work persistentInstance) {
        log.debug("deleting Work instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Work findById(java.lang.Integer id) {
        log.debug("getting Work instance with id: " + id);
        try {
            Work instance = (Work) getSession().get("com.lshtv.model.Work", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List findByExample(Work instance) {
        log.debug("finding Work instance by example");
        try {
            List results = getSession().createCriteria("com.lshtv.model.Work")
                .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List findByProperty(String propertyName, Object value) {
        log.debug("finding Work instance with property: " + propertyName + ", value: " + value);
        try {
            String queryString = "from Work as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByTitle(Object title) {
        return findByProperty(TITLE, title);
    }

    public List findByDescription(Object description) {
        return findByProperty(DESCRIPTION, description);
    }

    public List findByLink(Object link) {
        return findByProperty(LINK, link);
    }

    public List findByType(Object type) {
        return findByProperty(TYPE, type);
    }

    public List findByFiles(Object files) {
        return findByProperty(FILES, files);
    }

    public List findByItem(Object item) {
        return findByProperty(ITEM, item);
    }

    public List findBySequence(Object sequence) {
        return findByProperty(SEQUENCE, sequence);
    }

    /** 
     * @function 分页显示符合所有的记录数，将查询结果封装为Pager 
     * @param pageNo 
     *            当前页数 
     * @param pageSize 
     *            每页显示的条数 
     * @param map 
     *            将查询条件封装为map 
     * @return 查询结果Pager 
     */
    public List<Work> findTop() {
        String hql = "from Work group by item order by sequence desc";
        List list = this.getSession().createQuery(hql).list();
        return list;
    }

    /** 
     * @function 分页显示符合所有的记录数，将查询结果封装为Pager 
     * @param pageNo 
     *            当前页数 
     * @param pageSize 
     *            每页显示的条数 
     * @param map 
     *            将查询条件封装为map 
     * @return 查询结果Pager 
     */
    public PageList<Work> findPage(int pageNo, int pageSize, String type, String item) {
        PageList pager = null;
        String hql = "from Work where 1=1 ";
        if (type != null) {
            hql += " and tyle=" + type;
        }
        if (item != null) {
            hql += "and item=" + item;
        }
        Long count = (Long) this.getSession().createQuery("select count(*) " + hql).uniqueResult();
        List list = this.getSession().createQuery(hql + " order by sequence desc ")
            .setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
        return new PageList(pageSize, count.intValue(), pageNo, list);
    }

    public List findAll() {
        log.debug("finding all Work instances");
        try {
            String queryString = "from Work";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public Work work(Work detachedInstance) {
        log.debug("update Work instance");
        try {
            getSession().saveOrUpdate(detachedInstance);
            log.debug("merge successful");
            return detachedInstance;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Work merge(Work detachedInstance) {
        log.debug("merging Work instance");
        try {
            Work result = (Work) getSession().merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Work instance) {
        log.debug("attaching dirty Work instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Work instance) {
        log.debug("attaching clean Work instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}